package cmo.neutech.day09.ex;
import cmo.neutech.day09.ex.Student;

import java.util.Scanner;

public class StudentSystem {

    static Student[] students = {};

    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("1.查看所有学生信息");
            System.out.println("2.输入姓名搜索学生信息");
            System.out.println("3.增加学生信息");
            System.out.println("4.修改学生信息");
            System.out.println("5.删除学生信息");
            System.out.println("6.退出");
            System.out.println("请输入编号选择功能");
            int index = input.nextInt();
            if (index == 1) {
                selectAll();
            } else if (index == 2) {
                selectByName();
            } else if (index == 3) {
                add();
            } else if (index == 4) {

            } else if (index == 5) {

            } else if (index == 6) {
                break;
            } else {
                System.err.println("没有此功能");
            }
        }
    }

    /**
     *
     */
    public static void selectAll() {
        // 输出所有学生信息
        if (students.length == 0) {
            System.out.println("没有学生信息");
        } else {
            for (int i = 0; i < students.length; i++) {
                Student student = students[i];
                System.out.println("学号:" + student.getNo() + ",姓名:" + student.getName() + ",年龄:" + student.getAge()
                        + ",身高:" + student.getHeight() + ",性别:" + student.getSex());
            }
        }
    }

    public static void selectByName() {
        System.out.println("请输入要查询的学生姓名");
        String name = input.next();

        Student student = null;

        for (int i = 0; i < students.length; i++) {
            if (name.equals(students[i].getName())) {
                student = students[i];
                break;
            }
        }

        if (student == null) {
            System.out.println("没有此学生");
        } else {
            System.out.println("学号:" + student.getNo() + ",姓名:" + student.getName() + ",年龄:" + student.getAge()
                    + ",身高:" + student.getHeight() + ",性别:" + student.getSex());
        }
    }

    public static void add() {
        // 1.创建学生对象,但是此时学生对象里的属性都是默认值
        Student student = new Student();

        // 2.通过录入器给学生的属性录入值
        System.out.println("请输入学生编号");
        student.setNo(input.nextInt());

        // 3.校验是否重复等等
        boolean isFind = false;
        for (int i = 0; i < students.length; i++) {
            if (student.getNo() == students[i].getNo()) {
                isFind = true;
                break;
            }
        }

        if (isFind) {
            System.out.println("学号重复");
        } else {
            System.out.println("请输入姓名");
            student.setName(input.next());
            System.out.println("请输入年龄");
            student.setAge(input.nextInt());
            System.out.println("请输入身高");
            student.setHeight(input.nextDouble());
            System.out.println("请输入性别");
            student.setSex(input.next());

            // 4.扩容数组
            Student[] copy = new Student[students.length + 1];
            for (int i = 0; i < students.length; i++) {
                copy[i] = students[i];
            }
            students = copy;

            // 5.存入数组
            students[students.length - 1] = student;
        }
    }

}
